package ControlLayer;

import java.util.ArrayList;
import DBLayer.DBConnection;
import DBLayer.DBDiscount;
import DBLayer.IFDBDiscount;
import ModelLayer.Discount;

public class DiscountCtr {

	public Discount getDiscount(int discountId) {
		IFDBDiscount dbDiscount = new DBDiscount();
		return dbDiscount.getDiscount(discountId);
	}
	
	public void insertDiscount(double discountPercent, int requirePrice) {
		Discount discount = new Discount();
		discount.setDiscountPercentage(discountPercent);
		discount.setMinPriceRequired(requirePrice);
		try {
			DBConnection.startTransaction();
			DBDiscount dbDiscount = new DBDiscount();
			dbDiscount.insertDiscount(discount);
			DBConnection.commitTransaction();
		} catch (Exception e) {
			DBConnection.rollbackTransaction();
		}
	}
	
	public ArrayList<Discount> getAllDiscount() {
		IFDBDiscount dbDiscount = new DBDiscount();
		ArrayList<Discount> discounts = new ArrayList<Discount>();
		discounts = dbDiscount.getAllDiscount();
		return discounts;
	}
	
	public int updateDiscount(int discountId, double disoucntPercent, int minPrice) {
		IFDBDiscount dbDiscount = new DBDiscount();
		Discount discountObj = new Discount();
		discountObj.setDiscountId(discountId);
		discountObj.setDiscountPercentage(disoucntPercent);
		discountObj.setMinPriceRequired(minPrice);
		return dbDiscount.updateDiscount(discountObj);
	}
	
	public void deleteDiscount(int discountId) {
		IFDBDiscount dbDiscount = new DBDiscount();
		dbDiscount.deleteDiscount(discountId);
	}

}
